package com.ming.stringstudy.feedbacksystem.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.Map;

@Slf4j
public class JwtUtils {
    private static final String singKey = "aming";
    private static final Long expire = 43200000L;

    public static String generateJwt(Map<String,Object> claims){
        String jwt = Jwts.builder()
                .setClaims(claims)
                .signWith(SignatureAlgorithm.HS256, singKey)
                .setExpiration(new Date(System.currentTimeMillis() + expire))
                .compact();
        log.info(jwt);
        return jwt;
    }

    public static Claims parserJwt(String jws){
        Claims body = Jwts.parser()
                .setSigningKey(singKey)
                .parseClaimsJws(jws)
                .getBody();
        log.info(body.toString());
        return body;
    }
}
